<?php
include "ziplib.php";

class svn extends WikiPlugin
{
	function svn()
	{
		$this->add_macro('svnzip', 'svnzip');
		$this->add_action('download', 'downloadzip');
	}
	
	function getrev($f)
	{
		$filelisttxt = implode('', file("{$r}{$f}"));
		preg_match('#Revision ([0-9]+)#i', $filelisttxt, $revs);
		$this->rev = $revs[1];
	}

	function rzip($r, $f='')
	{
		$filelisttxt = implode('', file("{$r}{$f}"));
		preg_match_all('#href="([^/"]+)/?"#i', $filelisttxt, $files);
		preg_match('#Revision ([0-9]+)#i', $filelisttxt, $revs);
		$this->rev = $revs[1];
		foreach($files[1] as $file)
		{
			if($file != '..')
			{
				$data = implode('', file("{$r}{$f}/{$file}"));

				$data = preg_replace('#\$Id\$#i', "\$Id: {$file} {$this->rev} " . date('Y-m-d H:i:s') . " MicroWikiExport \$", $data);
				if(strstr($data, '<a href="http://subversion.tigris.org/">Subversion</a>'))
				{
					//echo "Adding directory: {$f}/{$file}\n";
					$this->rzip($r, "{$f}/{$file}");
				}
				else
				{
					$fname = "{$f}/{$file}";
					if($fname{0} == '/') $fname = substr($fname, 1);
					//echo "Adding file: {$f}/{$file}\n";
					$this->zipfile->zl_add_file($data, $fname, 'g9');
				}
			}
		}
	}

	function svnzip($reponame, $repo) 
	{
		global $rewrite, $wiki;
		return $rewrite->get_link(array('page'=>$wiki->page_name, 'action'=>'download', 'tag'=>$reponame));
	}
	
	
	
	function downloadzip()
	{
		global $wiki, $db, $mwconfig, $tag, $page, $rewrite;
		
		if(!$wiki->edit_mode) {
			if($body = $db->get_var("SELECT body FROM {$mwconfig->db_tableprefix}pages WHERE (pagename LIKE '{$page}') ORDER BY postdate DESC LIMIT 1;")) {
				if(preg_match('/{{svnzip:' . $tag . '\|(.*?)}}/i', $body, $matches)) {
					$repo = $matches[1];
					
					$this->getrev($repo);
					$wiki->cache_off = false;
					$cache = $wiki->get_cache("svn-" .  md5("{$repo}"));
					$zipfilename = $mwconfig->imagedir . "/svn/{$tag}.zip";
					if((intval($cache) < intval($this->rev)) || (!file_exists($zipfilename))) {
						if($wiki->rmkdir(dirname($zipfilename))) {
							if($fp = fopen($zipfilename, 'w+')) {
								$this->zipfile = new Ziplib();
								$this->rzip($repo);

								fwrite($fp, $this->zipfile->zl_pack("This archive contains the contents of the subversion repository at {$repo}, and was downloaded from " . $rewrite->get_link(array('page'=>$page)) . " ."));
								fclose($fp);
							}
							else {
								die("Could not save zip file to cache.");
							}
						}
						else {
							die("Could not create zip file directory.");
						}
					}
					
					$wiki->add_cache("svn-" .  md5("{$repo}"), $page, intval($this->rev));
					ob_end_clean();
					
					header("content-type: application/zip");
					header("Content-Disposition: attachment; filename=\"{$tag}.zip\"");
					echo implode('', file($zipfilename));
					die();
				}
				else {
					die("Could not find tag \"{$tag}\".");
				}
			}
			else {
				die("Could not find page {$page}.");
			}
		}
	}
}

?>